﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace HousingDistribution
{
    public partial class FormBothEmpScreening : Form
    {
        public FormBothEmpScreening()
        {
            InitializeComponent();
            //处理界面布局
            this.dataGridView.Dock = DockStyle.Fill;
            this.dataGridView.Anchor = ((AnchorStyles)
                                        ((((AnchorStyles.Top
                                          | AnchorStyles.Bottom)
                                          | AnchorStyles.Left)
                                          | AnchorStyles.Right)));
           
            this.BtnClose.Anchor = AnchorStyles.Bottom | AnchorStyles.Right;
        }

        private void FormBothEmpScreening_Load(object sender, EventArgs e)
        {
            this.Dock = DockStyle.Fill;

            InitDataTable(bothEmpTable);
            dataGridView.DataSource = bothEmpTable;

            ReadData();
        }

        private void ReadData()
        {
            string sqlStr = "select * from person;";

            DataSet ds = DBProc.Instance().ReadData(sqlStr);

            if (ds.Tables.Count == 0)
                return;

            foreach (DataRow row in ds.Tables[0].Rows)
            {
                PersonInfo info = parser.ParsePerson(row);

                if (info.name == null)
                    continue;

                //首先在 mateIDs 中查找配偶信息是否已经找到，避免查找两次
                if (mateIDs.Contains(info.idperson))
                    continue;

                PersonInfo mateInfo = RedundantrDataProc.Instance().GetMateInfoInMySql(info);
                if (mateInfo.idperson> 0)
                {
                    ProcBothEmployee(info, mateInfo, ref bothEmpTable);

                    mateIDs.Add(mateInfo.idperson);
                    p2 = mateInfo.idperson;
                    p1 = info.idperson;

                    string sqlUpdate2 = "update person set bothEmployee = '1' where idperson = '" + p1 + "'";
                    string sqlUpdate3 = "update person set bothEmployee = '1' where idperson = '" + p2 + "'";

                    DBProc.Instance().Excute(sqlUpdate2.ToString());
                    DBProc.Instance().Excute(sqlUpdate3.ToString());
                }             
            }
        }

        private void buttonClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        int p1 = 0;
        int p2 = 0;

        private void dataGridView_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            int rowIndex = e.RowIndex;
            int colIndex = e.ColumnIndex;

            if (rowIndex < 0 || colIndex < 0)
                return;

            DataRow dr = bothEmpTable.Rows[rowIndex];
            DataColumn dc = bothEmpTable.Columns[colIndex];

            string colTitle = dataGridView.Columns[colIndex].HeaderText.ToString();

            bool check;
            int personID = 0;

            //不是“选房资格”的item无效
            if (colTitle.Equals(ColNameConsts.HOUSING_ELIGIBLE_L))
            {
                check = Convert.ToBoolean(dataGridView.Rows[rowIndex].Cells[colIndex].EditedFormattedValue);
                personID = Convert.ToInt16(dataGridView.Rows[rowIndex].Cells[ColNameConsts.ID_L].EditedFormattedValue);
            }
            else if (colTitle.Equals(ColNameConsts.HOUSING_ELIGIBLE_R))
            {
                check = Convert.ToBoolean(dataGridView.Rows[rowIndex].Cells[colIndex].EditedFormattedValue);
                personID = Convert.ToInt16(dataGridView.Rows[rowIndex].Cells[ColNameConsts.ID_R].EditedFormattedValue);
            }
            else
                return;
            
            StringBuilder sqlUpdate = new StringBuilder("update person set housingEligible = '");
                
            sqlUpdate.Append(check ? "1'" : "0'");

            sqlUpdate.Append(" where idperson ='");
            sqlUpdate.Append(personID);
            sqlUpdate.Append("'");
            
            try
            {
                DBProc.Instance().Excute(sqlUpdate.ToString());
            }
            catch (Exception ex)
            {
                System.Windows.Forms.MessageBox.Show(ex.Message);
            }
        }

        private void dataGridView_DataBindingComplete(object sender, DataGridViewBindingCompleteEventArgs e)
        {
            //隐藏列（id 不需要显示）
            ((DataGridView)sender).Columns[ColNameConsts.ID_L].Visible = false;
            ((DataGridView)sender).Columns[ColNameConsts.ID_R].Visible = false;

            ((DataGridView)sender).Columns[ColNameConsts.NAME_L].DefaultCellStyle.BackColor = Color.LightBlue;
            ((DataGridView)sender).Columns[ColNameConsts.HOUSING_ELIGIBLE_L].DefaultCellStyle.BackColor = Color.LightBlue;
            ((DataGridView)sender).Columns[ColNameConsts.UNIT_L].DefaultCellStyle.BackColor = Color.LightBlue;
            ((DataGridView)sender).Columns[ColNameConsts.ADMIN_DUTY_L].DefaultCellStyle.BackColor = Color.LightBlue;
            ((DataGridView)sender).Columns[ColNameConsts.TECH_RANK_L].DefaultCellStyle.BackColor = Color.LightBlue;
            ((DataGridView)sender).Columns[ColNameConsts.TECH_TITLE_L].DefaultCellStyle.BackColor = Color.LightBlue;
        }

        void ProcBothEmployee(PersonInfo info, PersonInfo mateInfo, ref DataTable dt)
        {
            DataRow dr = dt.NewRow();
            dt.Rows.Add(dr);

            dr[ColNameConsts.ID_L] = info.idperson;
            dr[ColNameConsts.NAME_L] = info.name;
            dr[ColNameConsts.HOUSING_ELIGIBLE_L] = (info.housingEligible == 1);
            dr[ColNameConsts.UNIT_L] = info.unit;
            dr[ColNameConsts.ADMIN_DUTY_L] = info.adminDuty;
            dr[ColNameConsts.TECH_RANK_L] = info.techRank;
            dr[ColNameConsts.TECH_TITLE_L] = info.techTitle;

            dr[ColNameConsts.ID_R] = mateInfo.idperson;
            dr[ColNameConsts.NAME_R] = mateInfo.name;
            dr[ColNameConsts.HOUSING_ELIGIBLE_R] = (mateInfo.housingEligible == 1);
            dr[ColNameConsts.UNIT_R] = mateInfo.unit;
            dr[ColNameConsts.ADMIN_DUTY_R] = mateInfo.adminDuty;
            dr[ColNameConsts.TECH_RANK_R] = mateInfo.techRank;
            dr[ColNameConsts.TECH_TITLE_R] = mateInfo.techTitle;
        }

        void InitDataTable(DataTable dt)
        {
            dt.Columns.Add(ColNameConsts.ID_L, typeof(int));
            dt.Columns.Add(ColNameConsts.NAME_L, typeof(string));
            dt.Columns.Add(ColNameConsts.HOUSING_ELIGIBLE_L, typeof(System.Boolean));
            dt.Columns.Add(ColNameConsts.UNIT_L, typeof(string));
            dt.Columns.Add(ColNameConsts.ADMIN_DUTY_L, typeof(string));
            dt.Columns.Add(ColNameConsts.TECH_RANK_L, typeof(string));
            dt.Columns.Add(ColNameConsts.TECH_TITLE_L, typeof(string));

            dt.Columns.Add(ColNameConsts.ID_R, typeof(int));
            dt.Columns.Add(ColNameConsts.NAME_R, typeof(string));
            dt.Columns.Add(ColNameConsts.HOUSING_ELIGIBLE_R, typeof(System.Boolean));
            dt.Columns.Add(ColNameConsts.UNIT_R, typeof(string));
            dt.Columns.Add(ColNameConsts.ADMIN_DUTY_R, typeof(string));
            dt.Columns.Add(ColNameConsts.TECH_RANK_R, typeof(string));
            dt.Columns.Add(ColNameConsts.TECH_TITLE_R, typeof(string));      
        }

        ObjectParser parser = new PersonParserFromMySql();
        DataTable bothEmpTable = new DataTable();
        List<int> mateIDs = new List<int>();

        //数据库字段常量定义
        static class ColNameConsts
        {
            public const string ID_L = "索引";
            public const string NAME_L = "姓名";
            public const string UNIT_L = "单位";
            public const string HOUSING_ELIGIBLE_L = "分房资格";
            public const string ADMIN_DUTY_L = "职级";
            public const string TECH_RANK_L = "技术级别";
            public const string TECH_TITLE_L = "技术职称";

            public const string ID_R = "配偶索引";
            public const string NAME_R = "配偶姓名";
            public const string HOUSING_ELIGIBLE_R = "配偶分房资格";
            public const string UNIT_R = "配偶单位";
            public const string ADMIN_DUTY_R = "配偶职级";
            public const string TECH_RANK_R = "配偶技术级别";
            public const string TECH_TITLE_R = "配偶技术职称";
        }
    }
}
